\section{Mock Objects}

\begin{frame}[fragile]
\frametitle{Unit tests}
\begin{lstlisting}[basicstyle=\scriptsize, numbers=none]
public class StateTest extends TestCase {
    private static String TABLE = "Table";
    private Warehouse warehouse = new WarehouseImpl();

    protected void setUp() throws Exception {
        warehouse.add(TABLE, 50);
    }
    
    public void testOrderIsFilledIfEnoughInWarehouse() {
        Order order = new Order(TABLE, 50);
        order.fill(warehouse);
        assertTrue(order.isFilled());
        assertEquals(0, warehouse.getInventory(TABLE));
    }

\end{lstlisting}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}[fragile]
\frametitle{Mock tests}
\begin{lstlisting}[basicstyle=\scriptsize, numbers=none]
public class BehaviourTest extends MockObjectTestCase {
    private static String TABLE = "Table";

    public void testFillingRemovesInventoryIfInStock() {
        Order order = new Order(TABLE, 50);
        Mock warehouseMock = new Mock(Warehouse.class);

        warehouseMock.expects(once()).method("hasInventory")
          .with(eq(TABLE),eq(50))
          .will(returnValue(true));
        warehouseMock.expects(once()).method("remove")
          .with(eq(TABLE), eq(50))
          .after("hasInventory");

        order.fill((Warehouse) warehouseMock.proxy());
        warehouseMock.verify();
        assertTrue(order.isFilled());
    }

\end{lstlisting}
\end{frame}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{frame}{Test doubles}
Different kinds of \textit{pretend objects}:

\begin{description}
\item[dummy] is passed around to fill parameter list,
\item[fake] is simplified and not suitable for production,
\item[stub] provides a~fixed result to calls during the test,
\item[mock] has pre-programmed calls expected to receive.
\end{description}

\end{frame}

